package com.wbao.dianniu.manager;

import java.io.UnsupportedEncodingException;

import org.apache.http.entity.StringEntity;

import android.content.Context;

import com.wbao.dianniu.http.Batch;
import com.wbao.dianniu.http.Command;
import com.wbao.dianniu.http.HttpUtil;
import com.wbao.dianniu.http.HttpUtil.MyResponse;
import com.wbao.dianniu.listener.IAuthCode;
import com.wbao.dianniu.listener.IAuthCodeListener;
import com.wbao.dianniu.listener.IRegister;
import com.wbao.dianniu.listener.IRegisterListener;
import com.wbao.dianniu.logical.Action;
import com.wbao.dianniu.utils.Utils;

public class RegisterManager implements IRegister, IAuthCode {

    private IRegisterListener mListener = null;
    private IAuthCodeListener authListener = null;
    private Context mContext;

    public RegisterManager(Context context) {
        mContext = context;
    }

    @Override
    public void getAuthCode(Context context, String mobile) {
        StringEntity stringEntity = null;
        try {
            Command command = new Command();
            command.addArgs("mobile", mobile).addAction(Action.ACTION_GETCODE);
            stringEntity = new StringEntity(Batch.build().addCommand(command).toJson(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        HttpUtil http = new HttpUtil();
        http.req(mContext, stringEntity, new MyResponse() {

            @Override
            public void responseSucess(Object obj) {
                // TODO Auto-generated method stub
                if (null != authListener)
                    authListener.onAuthCodeSuccess(0);
            }

            @Override
            public void responseFailure(int errorCode, String msg) {
                // TODO Auto-generated method stub
                if (null != authListener)
                    authListener.onAuthCodeFailure(errorCode, msg);
            }
        });
    }

    @Override
    public void register(Context context, String mobile, String code, String password) {
        StringEntity stringEntity = null;
        try {
            Command command = new Command();
            command.addArgs("mobile", mobile)
                    .addArgs("pwd", Utils.Md5(password))
                    .addArgs("channel", "app")
                    .addArgs("code", code)
                    .addAction(Action.ACTION_REGISTER);
            stringEntity = new StringEntity(Batch.build().addCommand(command).toJson(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        HttpUtil http = new HttpUtil();
        http.req(mContext, stringEntity, new MyResponse() {

            @Override
            public void responseSucess(Object obj) {
                // TODO Auto-generated method stub
                if (null != mListener)
                    mListener.onRegisterSuccess();
            }

            @Override
            public void responseFailure(int errorCode, String msg) {
                // TODO Auto-generated method stub
                if (null != mListener)
                    mListener.onRegisterFailure(errorCode, msg);
            }
        });
    }

    @Override
    public boolean addListener(IRegisterListener listener) {
        mListener = listener;
        return mListener != null;
    }

    @Override
    public boolean removeListener(IRegisterListener listener) {
        if (null == listener)
            return false;
        mListener = null;
        return true;
    }

    @Override
    public boolean addAuthListener(IAuthCodeListener listener) {
        authListener = listener;
        return authListener != null;
    }

    @Override
    public boolean removeAuthListener(IAuthCodeListener listener) {
        if (null == listener)
            return false;
        authListener = null;
        return true;
    }


}
